/*
 * Flotta.cc
 *
 *  Created on: 2009.12.13.
 *      Author: zsanka
 */

#include "Flotta.hh"
#include "../Network/XmlClient.hh"

using namespace Gui;
using namespace Geom;
using namespace Network;

QImage Flotta::img("img/flotta.jpg");

Flotta::Flotta(QWidget* parent, StateChanged::FleetInfo fi, QString pl):
	QWidget(parent),
	playername(pl),
	selected(false),
	fi(fi)
{
	resize(64, 64);
	Vector<> const sizeVec(parent->size().width(), parent->size().height());
	Vector<> const v = server_to_client(fi.position, sizeVec);
	move(v.x, v.y);
}

void Flotta::paintEvent(QPaintEvent* ev) {
	DONTCARE(ev);
	QPainter painter(this);
	QPen pen(QColor(255, 0, 0));
	QPen pen2(QColor(0, 100, 255));
	QFont font = painter.font();
	font.setPointSize(font.pointSize() + 3);
	painter.setFont(font);
	QString self = XmlClient::instance().get_name();

	QRect rect;
	rect.setSize(size());

	painter.setPen(pen2);
	painter.drawImage(rect, img);
	rect.adjust(0,0,-1,-1);

	if(self == playername)
	{

		painter.setPen(pen2);
		painter.drawEllipse(rect);
	}
	if (selected) {
		painter.setPen(pen);
		painter.drawEllipse(rect);
	}

	painter.drawText(rect.bottomLeft(), playername);
}

void Flotta::mousePressEvent(QMouseEvent* ev) {
    if (ev->buttons() & Qt::LeftButton) {
        emit left_clicked(this);
    }

    if (ev->buttons() & Qt::RightButton) {
        emit right_clicked(this);
    }
}

void Flotta::select() {
    selected = true;
    repaint();
}

void Flotta::deselect() {
	selected = false;
	repaint();
}

u8 Flotta::get_id() const {
    return fi.id;
}

QString Flotta::get_player() const {
    return playername;
}
